package vip.specimen.system.controller.article;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.*;
import vip.specimen.system.common.annotation.SystemJurisdiction;
import vip.specimen.system.common.enums.ArticleStateEnum;
import vip.specimen.system.common.enums.ResultCode;
import vip.specimen.system.datasource.entity.SystemArticleEntity;
import vip.specimen.system.datasource.validation.SystemArticleAttributeValidation;
import vip.specimen.system.datasource.validation.SystemArticleValidation;
import vip.specimen.system.result.JsonResult;
import vip.specimen.system.service.SystemArticleService;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Set;
import java.util.UUID;

/**
 * *@Author: 余浪人
 * *@Date：2021/4/7 14:01
 * *@Email:yulangren520@Gmail.com
 * *@QQ:1368419640
 **/
@Api(tags = "内容管理")
@RestController
@RequestMapping(value = "/article/article")
public class ArticleController {
    @Resource
    private SystemArticleService articleService;

    @PutMapping
    @RequiresPermissions("article:article:insert")
    @ApiOperation(value = "新增", notes = "新增内容")
    @SystemJurisdiction(resourceType = "button", permissionName = "添加内容")
    public JsonResult<SystemArticleEntity> insert(@RequestBody @Valid SystemArticleValidation articleValidation) {
        return JsonResult.success(this.articleService.insert(articleValidation));
    }

    @GetMapping(value = "stateAll")
    @RequiresPermissions("article:article:select:state")
    @ApiOperation(value = "查看全部", notes = "查看全部状态")
    @SystemJurisdiction(resourceType = "button", permissionName = "查看全部状态")
    public JsonResult state() {
        ArrayList<HashMap<String, Object>> list = new ArrayList<>();
        for (ArticleStateEnum state : ArticleStateEnum.values()) {
            HashMap<String, Object> map = new HashMap<>();
            map.put("value", state.getState());
            map.put("stateName", state.getMsg());
            map.put("name", state.name());
            list.add(map);
        }
        return JsonResult.success(list);
    }

    @GetMapping
    @RequiresPermissions(value = "article:article:select")
    @ApiOperation(value = "查看", notes = "获取内容列表")
    @SystemJurisdiction(resourceType = "button", permissionName = "查看内容列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page", value = "页码", dataType = "int"),
            @ApiImplicitParam(name = "pageSize", value = "每页记录条数", dataType = "int")
    })
    public JsonResult<Page<SystemArticleEntity>> articlePageAll() {
        return JsonResult.success(this.articleService.findAllPage());
    }


    @PostMapping(value = "enable")
    @RequiresPermissions(value = "article:article:update:enable")
    @ApiOperation(value = "更新状态", notes = "根据uuid更新内容状态")
    @SystemJurisdiction(resourceType = "button", permissionName = "更新内容状态")
    public JsonResult<SystemArticleEntity> update(@RequestParam("uuid") UUID uuid) {
        SystemArticleEntity result = this.articleService.update(uuid);
        return null == result ? JsonResult.failed(ResultCode.ERROR_SQL_UPDATE_SERVER) : JsonResult.success(result);
    }

    @PostMapping(value = "attribute")
    @RequiresPermissions(value = "article:article:update:attribute")
    @ApiOperation(value = "更新属性", notes = "根据uuid更新内容属性")
    @SystemJurisdiction(resourceType = "button", permissionName = "更新内容属性")
    public JsonResult<SystemArticleEntity> update(@RequestBody @Valid SystemArticleAttributeValidation attributeValidation) {
        SystemArticleEntity result = this.articleService.update(attributeValidation);
        return null == result ? JsonResult.failed(ResultCode.ERROR_SQL_UPDATE_SERVER) : JsonResult.success(result);
    }

    @PostMapping
    @RequiresPermissions(value = "article:article:update")
    @ApiOperation(value = "更新", notes = "根据uuid更新内容")
    @SystemJurisdiction(resourceType = "button", permissionName = "更新内容")
    public JsonResult<SystemArticleEntity> update(@RequestParam("uuid") UUID uuid, @RequestBody @Valid SystemArticleValidation articleValidation) {
        SystemArticleEntity result = this.articleService.update(uuid, articleValidation);
        return null == result ? JsonResult.failed(ResultCode.ERROR_SQL_UPDATE_SERVER) : JsonResult.success(result);
    }

    @DeleteMapping
    @RequiresPermissions(value = "article:article:delete")
    @ApiOperation(value = "删除", notes = "根据uuid删除内容")
    @SystemJurisdiction(resourceType = "button", permissionName = "删除内容")
    public JsonResult<Object> delete(@RequestBody HashMap<String, UUID> param) {
        return this.articleService.delete(param.get("uuid")) ? JsonResult.success() : JsonResult.failed(ResultCode.ERROR_DELETE);
    }

    @DeleteMapping(value = "batch")
    @RequiresPermissions(value = "article:article:delete:batch")
    @ApiOperation(value = "批量删除", notes = "根据uuid列表批量删除")
    @SystemJurisdiction(resourceType = "button", permissionName = "批量删除内容")
    public JsonResult<HashMap<String, Integer>> deletes(@RequestBody HashMap<String, Set<UUID>> param) {
        int result = this.articleService.delete(param.get("uuids"));
        return JsonResult.success(new HashMap<String, Integer>(1) {{
            put("successNum", result);
        }});
    }
}
